<?php
include_once (MODEL.'/Repository.php');
include_once (MODEL.'/ProfileModel.php');

class ProfileRepository extends Repository
{
	public $collection;
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function ViewInform($AccountID)
	{	
     		$temp = $this->DB->selectCollection('Account')->find(array('_id' => new MongoId($AccountID)));
     		$cursor = $temp->getNext();
			$inform = new ProfileModel();
			$inform->AccountID=$cursor['_id'];
			$inform->FirstName=	$cursor['FirstName'];
			$inform->MiddleName=$cursor['MiddleName'];
			$inform->LastName= $cursor['LastName'];
			$inform->Sex=	$cursor['Sex'];
			$inform->Occupation=	$cursor['Occupation'];
			$inform->City=	$cursor['City'];
			$inform->Company=	$cursor['Company'];
			$inform->Avatar=	$cursor['Avatar'];
			$inform->Country=	$cursor['Country'];
			$inform->Birthday = $cursor['Birthday'];
			$inform->Location=	$cursor['Location'];
			$inform->Password = $cursor['Password'];
			$inform->Email = $cursor['Email'];
			$inform->FullName =	$cursor['FirstName']." ".$cursor['MiddleName']." ".$cursor['LastName'];
		
		return $inform;
	}
	
	public function SelectByRank()
	{
		$temp = $this->DB->selectCollection('Account')->find()->limit(5);
		$listAccount = array();
		while($temp->hasNext()){
			$ByRank = new ProfileModel();
			$cursor = $temp->getNext();
			$ByRank->AccountID = $cursor['_id'];
			$ByRank->Avatar = $cursor['Avatar'];
			$ByRank->FullName =	$cursor['FirstName']." ".$cursor['LastName'];
			$ByRank->Rank = $cursor['Rank'];
			$listAccount[]  = $ByRank;
		}
		
		return $listAccount;
	}
	
	public function UpdateName($AccountID,$FirstName,$MiddleName,$LastName,$Sex,$Birthday)
	{	
		$sex = $Sex==="male"?true:false;
		$avatar = $AccountID.".jpg";
		$temp = $this->DB->selectCollection('Account')->update(array('_id'=>new MongoId($AccountID)),array('$set'=>array('FirstName'=>$FirstName,'MiddleName'=>$MiddleName,'LastName'=>$LastName,'Avatar'=>$avatar,'Birthday'=>$Birthday,'Sex'=>$Sex)),array("upsert"=>false));
	}
	public function UpdateJob($AccountID,$Occupation,$Company)
	{
		$temp = $this->DB->selectCollection('Account')->update(array('_id'=>new MongoId($AccountID)),array('$set'=>array('Occupation'=>$Occupation,'Company'=>$Company)),array("upsert"=>false));
	}
	public function UpdateAddress($AccountID,$Street,$City,$Country)
	{
		$temp = $this->DB->selectCollection('Account')->update(array('_id'=>new MongoId($AccountID)),array('$set'=>array('Location'=>$Street,'City'=>$City,'Country'=>$Country)),array("upsert"=>false));
	}
	public function ChangePass($AccountID,$newPassword)
	{
		$temp = $this->DB->selectCollection('Account')->update(array('_id'=>new MongoId($AccountID)),array('$set'=>array('Password'=>md5($newPassword))),array("upsert"=>false));
	}
	
}
